//
//  IMIBaseNSStringHelper.m
//  IMIBase
//
//  Created by Travis on 09-7-12.
//  Copyright 2009 imi.im All rights reserved.
//
#import <CommonCrypto/CommonDigest.h>
#import "IMIBase/IMIBaseNSStringHelper.h"
#import "IMIBase/Base64/GTMBase64.h"

@implementation NSString (IMIBase)

+ (NSString *) stringWithUUID
{
    CFUUIDRef uuid = CFUUIDCreate(nil);
    NSString *uuidString = (NSString *)CFUUIDCreateString(nil, uuid);
    CFRelease(uuid);
    return [uuidString autorelease];
}


- (NSString *) MD5{
	const char* string = [self UTF8String];
	unsigned char result[16];
	CC_MD5(string, strlen(string), result);
	NSString* hash = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
					  result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], 
					  result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]];
	
	return [hash lowercaseString];
}

+ (NSString *) MD5OfFile:(NSString*)path
{
	NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
	if( handle== nil ) return @"ERROR GETTING FILE MD5"; // file didnt exist
	
	CC_MD5_CTX md5;
	
	CC_MD5_Init(&md5);
	
	BOOL done = NO;
	while(!done)
	{
		NSData* fileData = [handle readDataOfLength: 3000 ];
		CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
		if( [fileData length] == 0 ) done = YES;
	}
	unsigned char digest[CC_MD5_DIGEST_LENGTH];
	CC_MD5_Final(digest, &md5);
	NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
				   digest[0], digest[1], 
				   digest[2], digest[3],
				   digest[4], digest[5],
				   digest[6], digest[7],
				   digest[8], digest[9],
				   digest[10], digest[11],
				   digest[12], digest[13],
				   digest[14], digest[15]];
	return s;
}

- (NSString *) encodeToBase64{
	NSString *r=[GTMBase64 stringByEncodingData:[NSData dataWithBytes:[self UTF8String] 
															   length:strlen([self UTF8String])]];
	return r;
}

- (NSString *) decodeFromBase64{
	NSString *r;
	if(self!=@""){
		NSData *data=[GTMBase64 decodeData:[NSData dataWithBytes:[self UTF8String] 
														  length:strlen([self UTF8String])]];
		r=[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
	}else {
		r=@"";
	}
	return r;
}

- (NSString *)trimWhitespace
{
	NSMutableString *mStr = [self mutableCopy];
	CFStringTrimWhitespace((CFMutableStringRef)mStr);
	
	NSString *result = [mStr copy];
	
	[mStr release];
	return [result autorelease];
}
#pragma mark CGPoint

- (CGPoint) toCGPoint{
	CGPoint p;
	
    NSArray *a=[self componentsSeparatedByString:@","];
	p.x=[[a objectAtIndex:0] floatValue];
	p.y=[[a objectAtIndex:1] floatValue];
	
    return p;
}

#pragma mark CGSize

- (CGSize) toCGSize{
	CGSize p;
	
    NSArray *a=[self componentsSeparatedByString:@","];
	p.width=[[a objectAtIndex:0] floatValue];
	p.height=[[a objectAtIndex:1] floatValue];
	
    return p;
}

#pragma mark File
//make sure the string self is a file path!
//return file name without extention, eg: I'm Sorry
- (NSString*)fileName{
	//TODO: 
	
	return nil;
}
//return file extention, eg: mp3,caf,png...
- (NSString*)fileType{
	return [self pathExtension];
}
//return file's parent folder, eg: /tmp/ab/
- (NSString*)fileParentFolder{
	//TODO: 
	//NSMutableArray *tmp=[NSMutableArray arrayWithArray:[self pathComponents]];
	return nil;
}
@end